<?php
class DepartmentController extends Zend_Controller_Action
{
	/**
	 * Main department category name
	 * @var string $_departmentName
	 */ 
	protected $_departmentName;

	/**
	 * 
	 * @var
	 */	
	protected $_redirector;

	/**
	 * Category model
	 * @var object $_categoryModel;
	 */
	protected $_categoryModel;

	/**
	 * Content model
	 * @var object $_contentModel;
	 */
	protected $_contentModel;

	/**	 
	 * Tags model
	 * @var object $_tagsModel
	 */
	protected $_tagsModel;

	/**
	 * Current department id
	 * @var int $_departmentId
	 */
	protected $_departmentId;

	/**
	 * Current department main category id
	 * @var int $_deparmentCategoryId
	 */ 
	protected $_departmentCategoryId;

	/**
	 * Root href of current department.
	 * For example: '/department/eem'
	 * @var $_departmentRootHref;
	 */
	protected $_departmentRootHref;

	protected $_departmentTitle;
	protected $_departmentModel;

	public function init() {
		$this->_departmentName  = $this->_getParam('dept_name');		
		$this->_currentCategory = $this->_getParam('category');
		$this->_redirector      = $this->_helper->getHelper('Redirector');		
		$this->_categoryModel   = new Application_Model_Categories();
		$this->_contentModel    = new Application_Model_Content();
		$this->_tagsModel       = new Application_Model_Tags();
		$this->_departmentModel = new Application_Model_Department();

		$this->_departmentId    = $this->_categoryModel->getDepartmentIdByCategoryAlias(
			$this->_departmentName
		);
		$this->_departmentCategoryId = $this->_categoryModel->getCategoryIdByAliasAndDepartmentId(
			$this->_departmentName, 
			$this->_departmentId
		);
		$this->_departmentTitle = $this->_departmentModel->getDepartmentTitleByAliasAndDepartmentId(
			$this->_departmentName, 
			$this->_departmentId	
		);
		$this->_departmentRootHref = '/department/' . $this->_departmentName;			

		//needed on each page for department menu
		//$this->view->departmentCategoryId = $this->_departmentCategoryId;
		//$this->view->departmentRootHref   = $this->_departmentRootHref;
		//$this->view->departmentTitle      = $this->_departmentTitle;
		
		$this->_helper->_block->add(
			'departmentMenu',
			array(
				'departmentTitle'      => $this->_departmentTitle,
				'departmentName'       => $this->_departmentName,
				'departmentRootHref'   => $this->_departmentRootHref,
				'departmentCategoryId' => $this->_departmentCategoryId
			)
		);
		$this->_helper->_block->add('mainVideo');
        $this->_helper->_block->add('tabs');
        $this->_helper->_block->add('socialWidget');
        $this->_helper->_block->add('PopularNews');
        $this->_helper->_block->add('announcements');
        $this->_helper->_block->add('sites');
        $this->_helper->_block->add('otherSites');
	}
	public function indexAction() {
		//redirect to the first category
		$firstCategory = $this->_categoryModel->getFirstDepartmentCategory($this->_departmentId);		
		if (!$firstCategory) $this->_redirector->setGoToSimple('index', 'index');		

		$routeAlias = array(
			'title'        => 'static',
			'feed'	       => 'feed',
			'static'       => 'static',
			'photogallery' => 'photogallery',
			'custom'       => 'custom'		
		);
		$this->_redirector->gotoRoute(
            array(
            	'category'  => $firstCategory['alias'],
            	'action'    => $routeAlias[$firstCategory['type']],
            	'dept_name' => $this->_departmentName),            
            'department_content'
        );
	}	

	public function feedAction() {						
		$currentCategoryId = $this->_categoryModel->getCategoryIdByAliasAndDepartmentId(
			$this->_currentCategory, 
			$this->_departmentId
		);		

		$allContent = $this->_contentModel->getContentByCategoryId(
			$currentCategoryId,
			$this->_departmentId
		);		

		$this->view->content = $this->_contentModel->makePreviewForFeed($allContent);				
		$this->view->departmentName = $this->_departmentName;		
	}

	public function postAction() {
		/*$post         = $this->_contentModel->getPostByAlias($this->_currentCategory);		
		$post['tags'] = $this->_contentModel->getPostTags($post['id']);

		$this->view->post = $post;

		$this->view->popularTags    = $this->_tagsModel->getPopularTags();
		$this->view->popularPosts   = $this->_contentModel->getPopularPosts(); 			
		$this->view->departmentName = $this->_departmentName;*/
		$this->view->category = $this->_currentCategory;
	}

	public function photogalleryAction() {
		echo 'photogallery';
	}

	public function staticAction() {		
		$this->view->content = $this->_contentModel->getPostByCategoryAlias(
			$this->_currentCategory, 
			$this->_departmentId
		);
	}

	public function customAction() {
		$alias = $this->_getParam('category');			
		$scriptPath = '/custom/' . $alias .'.phtml';		
		try {
			$this->renderScript($scriptPath);
		} catch (Exception $e) {
			$this->renderScript('/custom/default.phtml');
		}
	}
}